//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.Text;
namespace OpenLS.Vml
{
    public class Style
    {
        public static Style Parse(string style)
        {
            if (style == null)
                return null;
            Style result = new Style();
            foreach (string s in style.Split(separator))
            {
                if (string.IsNullOrEmpty(s))
                    continue;
                if (s.StartsWith("&#"))
                    continue;
                string[] ss = s.Split(':');
                var key = ss[0].Trim();
                if (result.Setters.ContainsKey(key))
                    continue;
                result.Setters.Add(key, ss[1]);
            }
            return result;
        }

        readonly Dictionary<string, string> _setters  = new Dictionary<string, string>();
        public Dictionary<string, string> Setters
        {
            get { return _setters; }
        }
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            bool first = true;
            foreach (KeyValuePair<string, string> vv in Setters)
            {
                if (!first)
                    sb.Append(separator);
                first = false;
                sb.Append(vv.Key + ":" + vv.Value);
            }
            return sb.ToString();
        }
        private const char separator = ';';

        public string GetValue(string key)
        {
            string result;
            if (Setters.TryGetValue(key, out result))
                return result;
            return null;
        }

        public void SetValue(string key, string value)
        {
            key = key.Trim();
            Setters.Remove(key);
            Setters.Add(key, value);
        }
    }
}
